// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/common/base/trace_config.h>
#include <pollux/common/file/file.h>
#include <pollux/common/file/file_systems.h>
#include <pollux/exec/split.h>
#include <pollux/serializers/presto_serializer.h>
#include <pollux/vector/vector_stream.h>

namespace kumo::pollux::exec {
    class Operator;
}

namespace kumo::pollux::exec::trace {
    /// Used to serialize and write the input vectors from a particular operator
    /// into a data file. Additionally, it creates a corresponding summary file that
    /// contains information such as peak memory, input rows, operator type, etc.
    class OperatorTraceInputWriter {
    public:
        /// 'traceOp' is the operator to trace. 'traceDir' specifies the trace
        /// directory for the operator.
        explicit OperatorTraceInputWriter(
            Operator *traceOp,
            std::string traceDir,
            memory::MemoryPool *pool,
            UpdateAndCheckTraceLimitCB updateAndCheckTraceLimitCB);

        /// Serializes rows and writes out each batch.
        void write(const RowVectorPtr &rows);

        /// Closes the data file and writes out the data summary.
        void finish();

    private:
        // Flushes the trace data summaries to the disk.
        void writeSummary() const;

        Operator *const traceOp_;
        const std::string traceDir_;
        // TODO: make 'useLosslessTimestamp' configuerable.
        const serializer::presto::PrestoVectorSerde::PrestoOptions options_ = {
            true,
            common::CompressionKind::CompressionKind_ZSTD,
            0.8,
            /*nullsFirst=*/true
        };
        const std::shared_ptr<filesystems::FileSystem> fs_;
        memory::MemoryPool *const pool_;
        VectorSerde *const serde_;
        const UpdateAndCheckTraceLimitCB updateAndCheckTraceLimitCB_;

        std::unique_ptr<WriteFile> traceFile_;
        std::unique_ptr<VectorStreamGroup> batch_;
        bool finished_{false};
    };

    /// Used to write the input splits during the execution of a traced 'TableScan'
    /// operator. Additionally, it creates a corresponding summary file that
    /// contains information such as peak memory, number of splits, etc.
    ///
    /// Currently, it only works with 'HiveConnectorSplit'. In the future, it will
    /// be extended to handle more types of splits, such as
    /// 'IcebergHiveConnectorSplit'.
    class OperatorTraceSplitWriter {
    public:
        explicit OperatorTraceSplitWriter(Operator *traceOp, std::string traceDir);

        /// Serializes and writes out each split. Each serialized split is immediately
        /// flushed to ensure that we can still replay a traced operator even if a
        /// crash occurs during execution.
        void write(const exec::Split &split) const;

        void finish();

    private:
        static std::unique_ptr<melon::IOBuf> serialize(const std::string &split);

        // Flushes the trace data summaries to the disk.
        void writeSummary() const;

        Operator *const traceOp_;
        const std::string traceDir_;
        const std::shared_ptr<filesystems::FileSystem> fs_;
        const std::unique_ptr<WriteFile> splitFile_;
        bool finished_{false};
    };
} // namespace kumo::pollux::exec::trace
